package com.dataseek.iot.sys.manager.handler;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dataseek.iot.core.constant.ResponseConst;
import com.dataseek.iot.core.response.ResponseEntity;
import com.dataseek.iot.core.handler.ServiceHandler;
import com.dataseek.iot.mysql.entity.Role;
import com.dataseek.iot.mysql.entity.User;
import com.dataseek.iot.mysql.service.impl.RoleServiceImpl;
import com.dataseek.iot.mysql.service.impl.UserServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

import java.util.List;
import java.util.Objects;

/**
 * @author cc
 * @Title: 角色服务handler
 * @Description: 角色服务handler
 * @date 2021/9/69:32 下午
 * @since 1.0
 */
@Component
public class RoleServiceHandler extends ServiceHandler<Role> {

    @Autowired
    RoleServiceImpl roleService;

    @Autowired
    UserServiceImpl userService;

    @Override
    public Mono<ResponseEntity> saveOne(Object entity) {
        ResponseEntity responseEntity = new ResponseEntity();
        Role role = (Role) entity;
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("role_code", role.getRoleCode());
        if(isExist(wrapper)){
            responseEntity.setCode(ResponseConst.DUPLICATION_CODE);
            responseEntity.setMsg("角色编码已经存在");
            responseEntity.setData(role.getRoleCode());
            return Mono.just(responseEntity);
        }
        wrapper.or(true).eq("role_name", role.getRoleName());
        if(isExist(wrapper)){
            responseEntity.setCode(ResponseConst.DUPLICATION_CODE);
            responseEntity.setMsg("角色名称已经存在");
            responseEntity.setData(role.getRoleName());
            return Mono.just(responseEntity);
        }
        Boolean b = roleService.save((Role) entity);
        return this.createResponse(b);
    }

    @Override
    public Mono<ResponseEntity> saveBatch(List lists) {

        Boolean b = roleService.saveBatch(lists);
        return this.createResponse(b);
    }



    @Override
    public Mono<ResponseEntity> removeById(String id) {
        ResponseEntity entity = new ResponseEntity();
        QueryWrapper queryWrapper  = new QueryWrapper();
        queryWrapper.eq("role_id", id);
        List list = userService.list(queryWrapper);
        if(list.size()>0) {
            entity.setCode(ResponseConst.CRUD_FAILED_CODE);
            entity.setMsg("待删除线路信息中有用户关联，请先修改相关用户");
            entity.setData(list);
            return Mono.just(entity);
        }else {
            Boolean b = roleService.removeById(id);
            return this.createResponse(b);
        }
    }

    @Override
    public Mono<ResponseEntity> removeAll(List ids) {
        ResponseEntity entity = new ResponseEntity();
        QueryWrapper queryWrapper  = new QueryWrapper();
        queryWrapper.in("role_id", ids);
        List list = userService.list(queryWrapper);
        if(list.size()>0){
            entity.setCode(ResponseConst.CRUD_FAILED_CODE);
            entity.setMsg("待删除角色有关联的用户，请先修改相关用户信息");
            entity.setData(list);
            return Mono.just(entity);
        }else{
            Boolean b = roleService.removeByIds(ids);
            return this.createResponse(b);
        }

    }

    @Override
    public Mono<ResponseEntity> updateOne(Object entity) {
        Boolean b = roleService.updateById((Role) entity);
        return this.createResponse(b);
    }

    @Override
    public Mono<ResponseEntity> updateBatch(List entities) {
        Boolean b =  roleService.updateBatchById(entities);
        return this.createResponse(b);
    }

    @Override
    public Mono<ResponseEntity> findOne(String id) {
        Role role = roleService.getById(id);
        return this.createResponse(role);
    }

    @Override
    public Mono<ResponseEntity> page(Page p) {
        Page rolePage = roleService.page(p);
        return this.createResponse(rolePage);
    }

    @Override
    public Mono<ResponseEntity> page(Page p, Wrapper wrapper) {
        Page rolePage = roleService.page(p, wrapper);
        return this.createResponse(rolePage);
    }

    @Override
    public Flux<ResponseEntity> find(List ids) {
        List<Role> lists = roleService.listByIds(ids);
        return this.createResponse(lists);
    }

    @Override
    public Flux<ResponseEntity> find(Wrapper wrapper) {
        List<Role> lists = roleService.list(wrapper);
        return this.createResponse(lists);
    }

    @Override
    public Flux findAll() {
        List<Role> lists = roleService.list();
        return this.createResponse(lists);
    }

    @Override
    public Mono<ResponseEntity> remove(Wrapper wrapper) {
        Boolean b = roleService.remove(wrapper);
        return this.createResponse(b);
    }

    @Override
    public Mono<ResponseEntity> remove(String id) {
        return this.removeById(id);
    }

    @Override
    public Mono<ResponseEntity> update(Wrapper wrapper) {
        Boolean b = roleService.update(wrapper);
        return this.createResponse(b);
    }

    @Override
    public Mono<ResponseEntity> defaultResponseOne(ResponseEntity responseEntity) {
        return Mono.just(responseEntity);
    }

    @Override
    public Flux<ResponseEntity> defaultResponseTwo(ResponseEntity responseEntity) {
        return Flux.just(responseEntity);
    }

    /**
     * 验证是否存在
     * @param wrapper
     * @return
     */
    public Boolean isExist(QueryWrapper wrapper){
        if(Objects.nonNull(roleService.getOne(wrapper))) return true;
        return false;
    }
}
